package com.hb.trade.server.project.notice.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.enums.CommonTypeEnum;
import com.hb.core.pojo.enums.DeployStatusEnum;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.notice.entity.Notice;
import com.hb.core.pojo.project.notice.entity.NoticeBidSection;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.entity.BidSection;
import com.hb.trade.server.project.notice.service.NoticeBidSectionService;
import com.hb.trade.server.project.notice.service.NoticeService;
import com.hb.trade.server.project.notice.service.TenderInviteEnterpriseService;
import com.hb.trade.server.project.proj.service.ProjectService;
import com.hb.trade.server.project.section.service.raw.BidSectionService;
import com.hb.trade.server.project.section.service.raw.SupplyBidSignupService;
import com.hb.trade.server.project.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 *
 * @Description: 变更公告及实现逻辑
 **/
@Slf4j
@Service
public class NoticeSaveChange extends NoticeSaveSection {

    protected final BidSectionService bidSectionService;
    protected final NoticeBidSectionService noticeBidSectionService;
    protected final ProjectService projectService;
    protected final SupplyBidSignupService supplyBidSignupService;
    protected final TenderInviteEnterpriseService tenderInviteEnterpriseService;


    public NoticeSaveChange(NoticeService noticeService,
                            BidSectionService bidSectionService,
                            NoticeBidSectionService noticeBidSectionService,
                            ProjectService projectService, SupplyBidSignupService supplyBidSignupService,
                            TenderInviteEnterpriseService tenderInviteEnterpriseService) {
        super(noticeService);
        this.bidSectionService = bidSectionService;
        this.noticeBidSectionService = noticeBidSectionService;
        this.projectService = projectService;
        this.supplyBidSignupService = supplyBidSignupService;
        this.tenderInviteEnterpriseService = tenderInviteEnterpriseService;

    }

    /**
     * 对公告执行保存
     *
     * @param noticeDTO
     * @return 保存公告结果, true: 保存成功, false: 保存失败
     */
    @Override
    public boolean saveNotice(NoticeDTO noticeDTO) {

        // 处理公告主体信息
        Long noticeId = noticeDTO.getId();
        if (noticeId != null) {
            DeployStatusEnum status = getStatus(noticeId);
            if (status == DeployStatusEnum.DEPLOYED) {
                log.warn("无法修改已发布的变更公告");
                return false;
            }

            Notice rawNotice = noticeService.getById(noticeId);
            noticeDTO.to(rawNotice);
            noticeService.updateById(rawNotice);
        } else {
            // 公告发布状态默认为未发布
            noticeDTO.setDeployStatus(DeployStatusEnum.UN_DEPLOYED.getCode());
            noticeDTO.buildBaseData(1);
            noticeService.save(noticeDTO);
        }


        // 处理标段信息
        List<BidSectionDTO> sections = noticeDTO.getBidSectionList();
        // 处理公告跟标段的关联
        List<Long> sectionIds = CollUtil.map(sections, MPCommonEntity::getId, true);


        if (CollUtil.isNotEmpty(sectionIds)) {
            // 更新标段, 公告中间表
            noticeBidSectionService.updateNoticeSections(noticeDTO.getId(), sectionIds);
        }

        // 判断是公开的还是邀请的  如果是邀请的需要更新邀请企业信息
        if (null != noticeDTO.getCommonType() && noticeDTO.getCommonType() == CommonTypeEnum.INVITE.getCode()) {
            tenderInviteEnterpriseService.updateInviteEnterprise(noticeDTO);
        }

        return true;

    }

    @Override
    public String deploy(Notice notice) {
        List<NoticeBidSection> noticeBidSections = noticeBidSectionService.listNoticeBidSectionByNoticeId(notice.getId());
        if (CollUtil.isEmpty(noticeBidSections)){
            return null;
        }
        BidSectionDTO sectionOptions = BeanUtil.copyProperties(notice.getSectionOptions(),BidSectionDTO.class);

        //查询是否需要报名
        boolean signUpFlag = projectService.signUpFlag(notice.getProjectId());
         //如果需要报名 并且报名时间修改的 需要把已经报名并且审核通过的供应商 招标人需要重新审核报名信息
         if (signUpFlag){
             for (NoticeBidSection noticeBidSection : noticeBidSections) {
                 BidSection bidSection = bidSectionService.getById(noticeBidSection.getSectionId());
                 if (sectionOptions == null){
                     break;
                 }
                 if (bidSection == null){
                     continue;
                 }
                 if (bidSection.getSignStartTime().equals(sectionOptions.getSignStartTime()) &&
                     bidSection.getSignEndTime().equals(sectionOptions.getSignEndTime())
                 ){
                     continue;
                 }
                 //更新投标人报名的审核状态
                 supplyBidSignupService.updateExamineStatus(bidSection.getId());
             }
         }

        //判断是否需要重新编制招标文件
           //如果需要重新编制招标文件
        if (Enums.equals(LogicEnum.YES,notice.getAgainFileFlag())){
            //置空招标文件的标段信息
            for (NoticeBidSection noticeBidSection : noticeBidSections) {
                bidSectionService.cancelRelateFile(noticeBidSection.getSectionId());
            }
        }

        if (sectionOptions == null){
            return null;
        }
        //如果不需要重新编制招标文件 更新招标文件的信息 则需要把公告的标段信息同步到标段中
        for (NoticeBidSection noticeBidSection : noticeBidSections) {
             sectionOptions.setId(noticeBidSection.getSectionId());
             bidSectionService.updateById(sectionOptions);

        }


        return null;
    }
}
